<?php
    class RevenueController extends Controller
    {   
        var $metaTitle = "mTop - Mạng kiếm tiền trực tuyến.";
        var $metaKeywords = "Bán game, Bán ứng dụng, Bán SMS Kute, Bán hotclip, Gắn kích hoạt apk, Gắn kích hoạt java ,  Kinh doanh sms";
        var $metaDescription = "Mạng kiếm tiền online của Wapmaster - Kinh doanh SMS  Content - Kinh doanh Game và Ứng dụng.";

        public function direct(){
            if(!isset($_SESSION["userId"])){
                $this->redirect(Url::createUrl("login/index"));
            }           
        }  

        public function actionIndex(){
            $this->direct();
            $this->linkCanoncical = Url::createUrl('revenue/index');
            $userId= $_SESSION["userId"];
            $day = date("d");
            $month = date("m");
            $year = date("Y");

            $rowsRevenue = Revenue::getRevenueStatisticByDay($userId,$day,$month,$year);
            $this->render("revenue",array("rowsRevenue"=>$rowsRevenue,"day"=>$day,"month"=>$month,"year"=>$year)); 
        }

        public function actionYesterday(){
            $this->direct();
            $this->linkCanoncical = Url::createUrl('revenue/yesterday');
            $userId= $_SESSION["userId"];
            $yesterday = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
            $day = date("d",$yesterday);
            $month = date("m",$yesterday);
            $year = date("Y",$yesterday);

            $rowsRevenue = Revenue::getRevenueStatisticByDay($userId,$day,$month,$year);
            $this->render("revenue",array("rowsRevenue"=>$rowsRevenue,"day"=>$day,"month"=>$month,"year"=>$year)); 
        }

        public function actionMonthYear(){
            $this->direct();
            $this->linkCanoncical = Url::createUrl('revenue/monthYear');
            $userId= $_SESSION["userId"];
            $day="";
            $month = isset($_POST["month"])?$_POST["month"]:date("m");
            $year = isset($_POST["year"])?$_POST["year"]:date("Y");


            $rowsRevenue = Revenue::getRevenueStatisticByMonth($userId,$month,$year);
            $this->render("revenue",array("rowsRevenue"=>$rowsRevenue,"day"=>$day,"month"=>$month,"year"=>$year)); 
        }


        public function actionMonth(){
            $this->direct();
            $this->linkCanoncical = Url::createUrl('revenue/month');
            $userId= $_SESSION["userId"];
            $day="";
            $month = date("m");
            $year = date("Y");

            $rowsRevenue = Revenue::getRevenueStatisticByMonth($userId,$month,$year);
            $this->render("revenue",array("rowsRevenue"=>$rowsRevenue,"day"=>$day,"month"=>$month,"year"=>$year)); 
        }

        public function actionPreMonth(){
            $this->direct();
            $this->linkCanoncical = Url::createUrl('revenue/preMonth');
            $userId= $_SESSION["userId"];
            $month = date("m");
            $year = date("Y");
            $day="";
            if($month==1) {
                $year = date("Y")-1;
                $month = 12;
            }else{
                $year = date("Y");
                $month = date("m")-1;
            }

            $rowsRevenue = Revenue::getRevenueStatisticByMonth($userId,$month,$year);
            $this->render("revenue",array("rowsRevenue"=>$rowsRevenue,"day"=>$day,"month"=>$month,"year"=>$year)); 
        }
        public function actionDetail()
        {
            /* Lấy tất cả đầu số trong dữ liệu */
            $serviceNumber = Revenue::getAllserviceNumber();

            $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
            $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
            $numberRecordPerPage = 50;

            $search = array();
            $search["userId"] = $_SESSION['userId'];
            $search["day"] = isset($_GET["day"]) ? trim(strip_tags($_GET["day"])) : "";
            $search["month"] = isset($_GET["month"]) ? trim(strip_tags($_GET["month"])) : date("m") ;        
            $search["year"] = isset($_GET["year"]) ? trim(strip_tags($_GET["year"])) : date("Y");        
            $search["type"] = isset($_GET["type"]) ? trim(strip_tags($_GET["type"])) : "";

            $sms = array();
            $totalSms = 0;        
            $numberPage = 0;

            /*
            * Lấy tỷ lệ ăn chia
            */   

            $rate_sms = Rate::getAllRateSms();

            /* Lấy log sms của thành viên này */
            $sms = Revenue::getReportByUser($search, $rate_sms, $currentPage, $numberRecordPerPage);            
            $totalSms = Revenue::getTotalReportByUser($search);            
            $numberPage = ceil($totalSms / $numberRecordPerPage);


            $this->render(
            "detail"
            , array(
            "sms"=>$sms
            , "totalSms"=>$totalSms
            , "numberPage"=>$numberPage
            , "currentPage"=>$currentPage
            )
            );   
        }
    } 
?>
